﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Windows.Themes;
using PSE.Deployment.ConfigurationManager.Controls.Validations;
using PSE.Deployment.SetupBase.Configuration;

namespace PSE.Deployment.ConfigurationManager.Controls
{
	/// <summary>
	/// UserControl que instancia um PasswordTextBox
	/// </summary>
	public class PasswordTextBox : Control
	{
		Color _originalColor;
		ClassicBorderDecorator PART_Bd;
		PasswordBox PART_pwdBox = null;
		XmlTag _tag = null;
		RegistryValue _reg = null;
		XmlAttribute _attribute = null;
		bool _isRequired;

		public bool IsRequired
		{
			get { return _isRequired; }
			set { _isRequired = value; }
		}
		public bool IsValid
		{
			get { return this.Validate(); }
		}

		private PasswordTextBox()
		{
			this.SetResourceReference(PasswordTextBox.StyleProperty, "PasswordConfiguration");
			this.GotKeyboardFocus += new System.Windows.Input.KeyboardFocusChangedEventHandler(PasswordTextBox_GotKeyboardFocus);

			_originalColor = new Color();
			_originalColor.A = 255;
			_originalColor.R = 153;
			_originalColor.G = 153;
			_originalColor.B = 153;
		}

		void PasswordTextBox_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
		{
			if(e.OldFocus != this.PART_pwdBox && (Keyboard.Modifiers == ModifierKeys.Shift && Keyboard.IsKeyDown(Key.Tab))
				|| e.OldFocus != this.PART_pwdBox && (Keyboard.IsKeyDown(Key.Tab))
				)
				PART_pwdBox.Focus();
		}

		void pwdBox_PasswordChanged(object sender, RoutedEventArgs e)
		{
			string password = ((PasswordBox)sender).Password;

			if (_tag != null)
				_tag.Value = password;
			else if (_reg != null)
				_reg.Value = password;
			else if (_attribute != null)
				_attribute.Value = password;
		}

		public PasswordTextBox(XmlTag tag) : this()
		{
			_tag = tag;
		}

		public PasswordTextBox(XmlAttribute attribute) : this()
		{
			_attribute = attribute;
		}

		public PasswordTextBox(RegistryValue reg) : this()
		{
			_reg = reg;
		}

		void _PreviewLostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
		{
			e.Handled = !this.Validate();
		}

		bool Validate()
		{
			if (IsRequired && SpecializedControlsValidations.IsEmpty(PART_pwdBox.Password, false))
			{
				PART_Bd.BorderBrush = Brushes.Red;
				PART_Bd.BorderThickness = new Thickness(2);
				PART_pwdBox.ToolTip = SpecializedControlsValidations.GetDescription(ErrorMessage.RequiredField);
				return false;
			}
			else
			{
				PART_Bd.BorderBrush = new SolidColorBrush(_originalColor);
				PART_Bd.BorderThickness = new Thickness(1);
				PART_pwdBox.ToolTip = null;
				return true;
			}
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			PART_Bd = base.GetTemplateChild("PART_Bd") as ClassicBorderDecorator;
			PART_pwdBox = base.GetTemplateChild("PART_pwdBox") as PasswordBox;

			PART_pwdBox.PasswordChanged += new RoutedEventHandler(pwdBox_PasswordChanged);

			if (_tag != null)
			{
				PART_pwdBox.Password = _tag.Value;
				this.IsRequired = _tag.IsRequired;
			}
			else if (_reg != null)
			{
				PART_pwdBox.Password = _reg.Value;
				this.IsRequired = _reg.IsRequired;
			}
			else if (_attribute != null)
			{
				PART_pwdBox.Password = _attribute.Value;
				this.IsRequired = _attribute.IsRequired;
			}

			if (this.IsRequired)
				PART_pwdBox.PreviewLostKeyboardFocus += new System.Windows.Input.KeyboardFocusChangedEventHandler(_PreviewLostKeyboardFocus);
		}
	}
}